package com.dq.service.set.impl;

import com.dq.context.Constant;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.OrgCustomerLevelQuery;
import com.dq.controller.in.OrgTimeQuantumQuery;
import com.dq.controller.in.request.OrgHonorWallRequest;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.impl.BaseDaoImpl;

import com.dq.domain.org.Org;
import com.dq.domain.org.OrgCustomerLevel;
import com.dq.domain.org.OrgHonorWall;
import com.dq.domain.org.OrgTimeQuantum;
import com.dq.domain.sys.section.SysSection;
import com.dq.domain.setup.ReturnCommission;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.repository.BaseRepositories;
import com.dq.service.set.OrgSetService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Author luchangtong
 * @Date 2020/1/2 18:53
 */
@Service
public class OrgSetServiceImpl extends BaseRepositories implements OrgSetService {
    @Override
    public Vo setOrgBedNum(Long num)  throws AdminException{
        SysSection section =shiroService.getUser().getSection();
        Org org = dao.get(Org.class, section.getId());
        org.setBedNum(num);
        orgRepository.save(org);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo edit(Org orgEdit) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        SysSection section =sysUser.getSection();
        if(sysUser == null){
            throw new AdminException("请重新登陆");
        }
        Org org = dao.get(Org.class, section.getId());
        orgEdit.setUpdateUser(sysUser);
        orgEdit.setLastModifyTime(System.currentTimeMillis());
        BeanUtils.copyProperties(orgEdit,org, BeanNullUtil.getNullPropertyNames(orgEdit));
        orgRepository.save(org);
        return new Vo(HttpStatus.OK);
    }

    @Override
    @Transactional
    public Vo addOrgHonorWall(OrgHonorWallRequest orgHonorWallRequest, Long staffUserId) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        SysSection section =sysUser.getSection();
        if(sysUser == null){
            throw new AdminException("请重新登陆");
        }
        StaffUser staffUser = dao.get(StaffUser.class,staffUserId);
        if(staffUser == null){
            throw new AdminException("请重新选择员工");
        }
        if(orgHonorWallRepository.findBySysDeptAndStaffUserAndDeletedFalse((Org) section,staffUser).size()>0){
            throw new AdminException("此技师已存在荣誉墙");
        }
        OrgHonorWall orgHonorWall = new OrgHonorWall();
        orgHonorWall.setStaffUser(staffUser);
        orgHonorWall.setSysDept((Org) section);

        orgHonorWall.setCreateUser(sysUser);
        orgHonorWall.setUpdateUser(sysUser);
        orgHonorWall.setCreateTime(System.currentTimeMillis());
        orgHonorWall.setLastModifyTime(System.currentTimeMillis());
        orgHonorWall.setVersion(Constant.VERSION_VALUE);
        orgHonorWallRepository.save(orgHonorWall);
        staffUser.setHonorWallInfomation(orgHonorWallRequest.getHonorWallInfomation());
        staffUser.setHonorWallStaffImage(orgHonorWallRequest.getHonorWallStaffImage());
        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    @Override
    @Transactional
    public Vo editOrgHonorWall(OrgHonorWallRequest orgHonorWallRequest, Long staffUserId,Long id) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        SysSection section =sysUser.getSection();
        if(sysUser == null){
            throw new AdminException("请重新登陆");
        }
        OrgHonorWall orgHonorWall = dao.get(OrgHonorWall.class,id);
        if(orgHonorWall == null){
            throw new AdminException("请重新选择");
        }
        StaffUser staffUser = dao.get(StaffUser.class,staffUserId);
        if(staffUser == null){
            throw new AdminException("请重新选择员工");
        }
        if(orgHonorWall.getStaffUser().getId() != staffUserId){
            if(orgHonorWallRepository.findBySysDeptAndStaffUserAndDeletedFalse((Org) section,staffUser).size()>0){
                throw new AdminException("此技师已存在荣誉墙");
            }
        }
        orgHonorWall.setStaffUser(staffUser);
        orgHonorWall.setSysDept((Org) section);

        orgHonorWall.setUpdateUser(sysUser);
        orgHonorWall.setLastModifyTime(System.currentTimeMillis());
        orgHonorWallRepository.save(orgHonorWall);
        staffUser.setHonorWallInfomation(orgHonorWallRequest.getHonorWallInfomation());
        staffUser.setHonorWallStaffImage(orgHonorWallRequest.getHonorWallStaffImage());
        staffUserRepository.save(staffUser);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo deleteOrgHonorWall(Long id) throws AdminException{
        OrgHonorWall orgHonorWall = dao.get(OrgHonorWall.class,id);
        if(orgHonorWall == null){
            throw new AdminException("请重新选择");
        }
        orgHonorWall.setDeleted(true);
        orgHonorWallRepository.save(orgHonorWall);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo orgTimeQuantumList(OrgTimeQuantumQuery orgTimeQuantumQuery) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        orgTimeQuantumQuery.format();
        Map<String, Object> query = orgTimeQuantumQuery.parseMap();
        if(orgTimeQuantumQuery.getOrgId() != null){
            query.put("sysDept_id",orgTimeQuantumQuery.getOrgId());
            query.remove("orgId");
        }
        if(orgTimeQuantumQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,orgTimeQuantumQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(orgTimeQuantumQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,orgTimeQuantumQuery.getEndTime());
            query.remove("endTime");
        }
        if(orgTimeQuantumQuery.getFlag() != null){
            if(!orgTimeQuantumQuery.getFlag()){
                query.remove("page");
                query.remove("size");
            }
        }
        query.remove("flag");
        List<OrgTimeQuantum> orgTimeQuantums = dao.find(dao.query2Hql(OrgTimeQuantum.class,query),query);
        Long count = dao.count(dao.count2Hql(OrgTimeQuantum.class,query),query);
        PageHelper.startPage(orgTimeQuantumQuery.getPage(),orgTimeQuantumQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<OrgTimeQuantum.Vo>) VO.toVOCollection(orgTimeQuantums));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public Vo editOrgTimeQuantum(OrgTimeQuantum orgTimeQuantumEdit,Long id) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        if(sysUser == null){
            throw new AdminException("请重新登陆");
        }
        OrgTimeQuantum orgTimeQuantum = dao.get(OrgTimeQuantum.class,id);
        if(orgTimeQuantum == null){
            throw new AdminException("请重新选择");
        }
        if(StringUtils.isEmpty(orgTimeQuantumEdit.getBeginTime())){
            throw new AdminException("开始时间不能为空");
        }
        if(StringUtils.isEmpty(orgTimeQuantumEdit.getEndTime())){
            throw new AdminException("结束时间不能为空");
        }
        orgTimeQuantumEdit.setSysDept((Org) sysUser.getSection());
        orgTimeQuantumEdit.setUpdateUser(sysUser);
        orgTimeQuantumEdit.setLastModifyTime(System.currentTimeMillis());

        BeanUtils.copyProperties(orgTimeQuantumEdit,orgTimeQuantum, BeanNullUtil.getNullPropertyNames(orgTimeQuantumEdit));
        orgTimeQuantumRepository.save(orgTimeQuantum);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo addOrgTimeQuantum(OrgTimeQuantum orgTimeQuantum) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        if(sysUser == null){
            throw new AdminException("请重新登陆");
        }
        if("".equals(orgTimeQuantum.getBeginTime()) && StringUtils.isEmpty(orgTimeQuantum.getBeginTime())){
            throw new AdminException("开始时间不能为空");
        }
        if("".equals(orgTimeQuantum.getEndTime()) && StringUtils.isEmpty(orgTimeQuantum.getEndTime())){
            throw new AdminException("结束时间不能为空");
        }
        orgTimeQuantum.setSysDept((Org) sysUser.getSection());

        orgTimeQuantum.setCreateUser(sysUser);
        orgTimeQuantum.setUpdateUser(sysUser);
        orgTimeQuantum.setCreateTime(System.currentTimeMillis());
        orgTimeQuantum.setLastModifyTime(System.currentTimeMillis());
        orgTimeQuantum.setVersion(Constant.VERSION_VALUE);
        orgTimeQuantumRepository.save(orgTimeQuantum);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo deleteOrgTimeQuantum(Long id) throws AdminException {
        OrgTimeQuantum orgTimeQuantum = dao.get(OrgTimeQuantum.class,id);
        if(orgTimeQuantum == null){
            throw new AdminException("请重新选择");
        }
        orgTimeQuantum.setDeleted(true);
        orgTimeQuantumRepository.save(orgTimeQuantum);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo orgCustomerLevelList(OrgCustomerLevelQuery orgCustomerLevelQuery) throws AdminException {
        SysSection section = shiroService.getDept();
        orgCustomerLevelQuery.format();
        Map<String, Object> query = orgCustomerLevelQuery.parseMap();
        query.put("sysDept", section);
        if(StringUtils.isNotEmpty(orgCustomerLevelQuery.getLevenName())){
            query.put("levenName"+BaseDaoImpl.likeSqlStr, BaseAdminQuery.likeSql(orgCustomerLevelQuery.getLevenName()));
            query.remove("levenName");
        }
        if(orgCustomerLevelQuery.getBeginTime()!=null){
            query.put("createTime"+ BaseDaoImpl.gteSqlStr,orgCustomerLevelQuery.getBeginTime());
            query.remove("beginTime");
        }
        if(orgCustomerLevelQuery.getEndTime()!=null){
            query.put("createTime"+BaseDaoImpl.lteSqlStr,orgCustomerLevelQuery.getEndTime());
            query.remove("endTime");
        }
        List<OrgCustomerLevel> orgCustomerLevels = dao.find(dao.query2Hql(OrgCustomerLevel.class, query), query);
        Long count = dao.count(dao.count2Hql(OrgCustomerLevel.class,query),query);
        PageHelper.startPage(orgCustomerLevelQuery.getPage(),orgCustomerLevelQuery.getSize());
        PageInfo pageInfo = new PageInfo((List<OrgCustomerLevel.Vo>) VO.toVOCollection(orgCustomerLevels));
        pageInfo.setTotal(count);
        return new Vo(pageInfo);
    }

    @Override
    public Vo addOrgCustomerLevel(OrgCustomerLevel orgCustomerLevel) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        if(sysUser.getSection() == null){
            throw new AdminException("门店异常");
        }

        check(orgCustomerLevel);

        orgCustomerLevel.setCreateTime(System.currentTimeMillis());
        orgCustomerLevel.setLastModifyTime(System.currentTimeMillis());
        orgCustomerLevel.setCreateUser(sysUser);
        orgCustomerLevel.setSysDept((Org) sysUser.getSection());
        orgCustomerLevel.setVersion(Constant.VERSION_VALUE);
        orgCustomerLevelRepository.save(orgCustomerLevel);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo editOrgCustomerLevel(Long id,OrgCustomerLevel orgCustomerLevelEdit) throws AdminException {
        SysUser sysUser = shiroService.getUser();
        OrgCustomerLevel orgCustomerLevel = dao.get(OrgCustomerLevel.class,id);
        if(orgCustomerLevel == null){
            throw new AdminException("请重新选择");
        }

        check(orgCustomerLevelEdit);

        orgCustomerLevelEdit.setSysDept((Org) sysUser.getSection());
        orgCustomerLevelEdit.setUpdateUser(sysUser);
        orgCustomerLevelEdit.setLastModifyTime(System.currentTimeMillis());

        BeanUtils.copyProperties(orgCustomerLevelEdit,orgCustomerLevel, BeanNullUtil.getNullPropertyNames(orgCustomerLevelEdit));
        orgCustomerLevelRepository.save(orgCustomerLevel);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo deleteOrgCustomerLevel(Long id) throws AdminException {
        OrgCustomerLevel orgCustomerLevel = dao.get(OrgCustomerLevel.class,id);
        if(orgCustomerLevel == null){
            throw new AdminException("请重新选择");
        }
        orgCustomerLevel.setDeleted(true);
        orgCustomerLevelRepository.save(orgCustomerLevel);
        return new Vo(HttpStatus.OK);
    }

    @Override
    public Vo getIntegalProportion(ReturnCommission.ProportionType proportionType) {
        SysSection section = shiroService.getDept();
        if(section == null){
            throw new AdminException("用户门店信息异常");
        }
        ReturnCommission returnCommission = returnCommissionRepository.getBySysDeptAndDeletedFalseAndProportionType((Org) section,proportionType);
        if(returnCommission == null){
            returnCommission = new ReturnCommission();
            returnCommission.setProportion(BigDecimal.ZERO);
        }
        return new Vo(returnCommission.toVO());
    }

    @Override
    public Vo setIntegalProportion(ReturnCommission.ProportionType proportionType, BigDecimal proportion) {
        SysSection section = shiroService.getDept();
        if(section == null){
            throw new AdminException("用户门店信息异常");
        }
        ReturnCommission returnCommission = returnCommissionRepository.getBySysDeptAndDeletedFalseAndProportionType((Org) section,proportionType);
        if(returnCommission == null){
            returnCommission = new ReturnCommission();
        }
        returnCommission.setSysDept((Org) section);
        returnCommission.setProportion(proportion);
        returnCommission.setProportionType(proportionType);
        returnCommissionRepository.save(returnCommission);
        return new Vo(HttpStatus.OK);
    }

    public void check(OrgCustomerLevel o) throws AdminException{
        if(StringUtils.isEmpty(o.getLevenName())){
            throw new AdminException("等级名称不能为空");
        }
        if(o.getMinAmount() == null){
            throw new AdminException("该等级满足最低消费金额为空");
        }
        if(o.getMaxAmount() == null){
            throw new AdminException("该等级满足最高消费金额");
        }
    }
}
